package com.gexin.controller.commom;

import com.alibaba.fastjson.JSONObject;
import com.gexin.common.AppGlobal;
import com.gexin.core.web.BaseController;
import com.gexin.model.order.OrderExpress;
import com.gexin.model.order.OrderMaster;
import com.gexin.model.weixin.WeixinUser;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.log.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * 快递的接口
 * @author fang 2016-06-24
 *
 */
@ControllerBind(controllerKey = "/api/express")
public class ExpressController extends BaseController {
	private static final Log log = Log.getLog(ExpressController.class);
	public  void test(){
		//renderText(WeixinUser.dao.cache_getUserSubscribeSate("oFcHvsgunqBDnlzFVWe4Wf-HtvPU")+"");
	}
	/**
	 * 
	 * 物流的ajax接口
	 */
	public  void index() {
		log.info("快递查询");
		log.info("订单id"+getPara("order_id"));
		String order_id=getPara("order_id");
		//Order order=Order.dao.findById(getPara("order_id"));
		OrderExpress orderExpress  = OrderExpress.dao.cache_getORderExpressByOrderId(order_id);
		renderJson(orderExpress);
	}

	/**
	 * 静态方法  dao  层  可以调用
	 * @param order_id
	 * @return
	 */
	public static JSONObject ajax_search(String express_name, String express_no) {
			log.info("远程接口调用快递查询");
			String content="";
			try {
				URL url = new URL("http://www.kuaidiapi.cn/rest/?uid=71530&key=9f99d28855b946c6a51d5433f8b8ab68&order="+express_no+"&id="+express_name);
				URLConnection con = url.openConnection();
				con.setAllowUserInteraction(false);
				InputStream urlStream = url.openStream();
				String type = URLConnection.guessContentTypeFromStream(urlStream);
				String charSet = null;
				if (type == null)
					type = con.getContentType();

				if (type == null || type.trim().length() == 0
						|| type.trim().indexOf("text/html") < 0)
					return null ;//

				if (type.indexOf("charset=") > 0)
					charSet = type.substring(type.indexOf("charset=") + 8);

				byte b[] = new byte[10000];
				int numRead = urlStream.read(b);
				 content = new String(b, 0, numRead);
				while (numRead != -1) {
					numRead = urlStream.read(b);
					if (numRead != -1) {
						// String newContent = new String(b, 0, numRead);
						String newContent = new String(b, 0, numRead, charSet);
						content += newContent;
					}
				}
				log.info("content:内容" + content);
				urlStream.close();
				
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}    
			JSONObject jsonObject2 = JSONObject.parseObject(content);
			//return JsonKit.toJson(jsonObject2);
			return jsonObject2;
		}
	
	
    //备份方法
	public void search() {
		log.info("快递查询");
		log.info("订单id"+getPara("order_id"));
		OrderMaster order= OrderMaster.dao.findById(getPara("order_id"));
		String express_name;
		String express_no;
		if((order==null || order.getOrderId()==null) && AppGlobal.dev_Mode){
			express_name="yunda";
			express_no="3101066750539";
		}else{
			express_name=order.getShippingCompName();
			express_no=order.getShippingSn();
		}
		try {
			// URL url= new
			// URL("http://api.kuaidi100.com/api?id=XXXX&com=tiantian&nu=11111&show=0&muti=1&order=desc");
			URL url = new URL("http://www.kuaidiapi.cn/rest/?uid=71530&key=9f99d28855b946c6a51d5433f8b8ab68&order="+express_no+"&id="+express_name);
			URLConnection con = url.openConnection();
			con.setAllowUserInteraction(false);
			InputStream urlStream = url.openStream();
			String type = URLConnection.guessContentTypeFromStream(urlStream);
			String charSet = null;
			if (type == null)
				type = con.getContentType();

			if (type == null || type.trim().length() == 0
					|| type.trim().indexOf("text/html") < 0)
				return;

			if (type.indexOf("charset=") > 0)
				charSet = type.substring(type.indexOf("charset=") + 8);

			byte b[] = new byte[10000];
			int numRead = urlStream.read(b);
			String content = new String(b, 0, numRead);
			while (numRead != -1) {
				numRead = urlStream.read(b);
				if (numRead != -1) {
					// String newContent = new String(b, 0, numRead);
					String newContent = new String(b, 0, numRead, charSet);
					content += newContent;
				}
			}
			System.out.println("content:内容" + content);
			JSONObject jsonObject = JSONObject.parseObject(content);
			String name = jsonObject.getString("status");
			System.out.println("状态 is:" + name);
			System.out.println("内容为:" + jsonObject.getString("data"));
			renderJson(content);
			// String address = jsonObject.getString("address");
			// System.out.println("address is:" + address);
			// JSONArray jsonArray = jsonObject.getJSONArray("array");
			// for (int i = 0; i < jsonArray.size(); i++) {
			// System.out.println(“item ” + i + ” :” + jsonArray.getString(i));
			// }
			//
			urlStream.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	//代码备份
	/*
	 //OrderExpress orderExpress = CacheUtils.get(RedisKeyConst.Order_Express+order.getId());
		/orderExpress=null;
		if(orderExpress==null){
			if(OrderExpress.dao.getOrderExpressByOrderId(order.getId())==null){
				JSONObject expressObject=ajax_search(order_id);
				String content=expressObject.getString("data");
				String status=expressObject.getString("status");
				OrderExpress orderExpress2=new OrderExpress();
				orderExpress2.setContent(content);
				orderExpress2.setExpressState(Integer.parseInt(status));
				orderExpress2.setOrderId(order_id);
				orderExpress2.setUpdateTime(new Date());
				orderExpress2.save();
				orderExpress=orderExpress2;
				CacheUtils.set(RedisKeyConst.Order_Express+order.getId(), orderExpress,1800);
			}	
		}
		else{
			if(OrderExpress.dao.getAlreadyOrderExpressByOrderId(order_id)!=null){
				orderExpress=OrderExpress.dao.getAlreadyOrderExpressByOrderId(order_id);
				CacheUtils.set(RedisKeyConst.Order_Express+order.getId(), orderExpress,1800);
			}
			else{
				JSONObject expressObject=ajax_search(order_id);
				String content=expressObject.getString("data");
				String status=expressObject.getString("status");
				OrderExpress orderExpress2=OrderExpress.dao.getOrderExpressByOrderId(order.getId());
				orderExpress2.setContent(content);
				orderExpress2.setExpressState(Integer.parseInt(status));
				orderExpress2.setOrderId(order_id);
				orderExpress2.setUpdateTime(new Date());
				orderExpress2.update();
				orderExpress=orderExpress2;
				CacheUtils.set(RedisKeyConst.Order_Express+order.getId(), orderExpress,1800);
			}
		}
	 
	 
	 */

}


